package cn.ujn.edu.sun_shine.controller;


import cn.ujn.edu.sun_shine.dto.CustomerInfo;

import cn.ujn.edu.sun_shine.entity.BaseDict;
import cn.ujn.edu.sun_shine.entity.Customer;
import cn.ujn.edu.sun_shine.service.BaseDictService;
import cn.ujn.edu.sun_shine.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 徐冠宇 DoDo
 * @since 2020-11-06
 */
@RequestMapping("/customer")
@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;


    @Autowired
    private BaseDictService baseDictService;


    @RequestMapping("/index")
    public String index(Model model) {
        //查询下拉框
        List<BaseDict> customerSources = baseDictService.list(new QueryWrapper<BaseDict>().eq("dict_type_name", "客户信息来源"));
        List<BaseDict> customerIndustries = baseDictService.list(new QueryWrapper<BaseDict>().eq("dict_type_name", "客户行业"));
        List<BaseDict> customerLevels = baseDictService.list(new QueryWrapper<BaseDict>().eq("dict_type_name", "客户级别"));


        model.addAttribute("customerIndustries", customerIndustries);
        model.addAttribute("customerLevels", customerLevels);
        model.addAttribute("customerSources", customerSources);
        return "customer";
    }

    //根据id获取对象
    @ResponseBody
    @RequestMapping("/getCustomerById")
    public Customer getCustomerById(@RequestParam("id") Integer id) {
        return customerService.getById(id);
    }

    //查询列表
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1", value = "") Integer currentPage,
                       @RequestParam(value = "custName", required = false, defaultValue = "") String custName,
                       @RequestParam(value = "custSource", required = false, defaultValue = "") String custSource,
                       @RequestParam(value = "custIndustry", required = false, defaultValue = "") String custIndustry,
                       @RequestParam(value = "custLevel", required = false, defaultValue = "") String custLevel) {

        //传递给前端分页url

        model.addAttribute("name",custName);
        model.addAttribute("industry",custIndustry);
        model.addAttribute("level",custLevel);
        model.addAttribute("source",custSource);
        //启动构造器
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        if (!custName.isEmpty()) {

            qw.eq("cust_name", custName);
        }
        if (!custIndustry.isEmpty()) {
            qw.eq("cust_industry", custIndustry);
        }
        if (!custLevel.isEmpty()) {
            qw.eq("cust_level", custLevel);
        }
        if (!custSource.isEmpty()) {
            qw.eq("cust_source", custSource);
        }
        Page<Customer> page = new Page<>(currentPage, 5);
        Long total = customerService.page(page, qw).getPages();
        if (total - currentPage >= 8) {

            model.addAttribute("start", currentPage);
            model.addAttribute("current", currentPage);
            model.addAttribute("total", currentPage + 8);


        } else {
            if(total>8)
                model.addAttribute("start", total - 8);
            else model.addAttribute("start", 1);
            model.addAttribute("current", currentPage);
            model.addAttribute("total", total);
        }
        model.addAttribute("final", total);
        //上一页
        Integer pre;
        if (currentPage > 1)
            pre = currentPage - 1;
        else pre = 1;
        model.addAttribute("pre", pre);
        //下一页
        Integer next;
        if (currentPage < total)
            next = currentPage + 1;
        else next = Math.toIntExact(total);
        model.addAttribute("next", next);
        //查询列表
        List<Customer> customers = customerService.page(page, qw).getRecords();
        List<CustomerInfo> customerInfos = new ArrayList<>();
        for (Customer customer : customers) {
            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.setCustomer(customer);
            //赋值来源
            BaseDict base1 = baseDictService.getOne(new QueryWrapper<BaseDict>().eq("dict_id", customer.getCustSource()));
            customerInfo.setCustSource(base1.getDictItemName());
            //赋值工厂
            BaseDict base2 = baseDictService.getOne(new QueryWrapper<BaseDict>().eq("dict_id", customer.getCustIndustry()));
            customerInfo.setCustIndustry(base2.getDictItemName());
            //赋值等级
            BaseDict base3 = baseDictService.getOne(new QueryWrapper<BaseDict>().eq("dict_id", customer.getCustLevel()));
            customerInfo.setCustLevel(base3.getDictItemName());
            customerInfos.add(customerInfo);

        }
        model.addAttribute("customerInfos", customerInfos);


        //}
        //查询下拉框
        List<BaseDict> customerSources = baseDictService.list(new QueryWrapper<BaseDict>().eq("dict_type_name", "客户信息来源"));
        List<BaseDict> customerIndustries = baseDictService.list(new QueryWrapper<BaseDict>().eq("dict_type_name", "客户行业"));
        List<BaseDict> customerLevels = baseDictService.list(new QueryWrapper<BaseDict>().eq("dict_type_name", "客户级别"));


        model.addAttribute("customerIndustries", customerIndustries);
        model.addAttribute("customerLevels", customerLevels);
        model.addAttribute("customerSources", customerSources);
        return "customer";
    }

    @RequestMapping("/create")
    @ResponseBody
    public String create(Customer customer) {
        boolean b = customerService.saveOrUpdate(customer);
        if (b) {
            return "OK";
        }
        return null;
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Customer customer) {
        boolean b = customerService.saveOrUpdate(customer);
        if (b) {
            return "OK";
        }
        return null;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id) {
        boolean b = customerService.removeById(id);
        if (b) {
            return "OK";
        }
        return null;
    }
}

